// ==UserScript==
// @name         Extract and Copy Cookies
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Add a button to extract and copy cookies
// @author       You
// @match        https://buyin.jinritemai.com/*
// @match        https://channels.weixin.qq.com/*
// @match        https://compass.jinritemai.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 创建一个按钮
    const button = document.createElement('button');
    button.textContent = 'Copy Cookies';
    button.style.position = 'fixed';
    button.style.left = '10px';
    button.style.bottom = '10px';
    button.style.zIndex = '10000';
    button.style.padding = '10px 20px';
    button.style.backgroundColor = '#4CAF50';
    button.style.color = 'white';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';
    button.style.fontSize = '16px';
    button.style.whiteSpace = 'nowrap';
    button.style.textOverflow = 'ellipsis';
    button.style.width = 'auto';
    button.style.height = 'auto';
    button.style.display = 'flex';
    button.style.justifyContent = 'center';
    button.style.alignItems = 'center';
    document.body.appendChild(button);

    // 拖动功能
    let isDragging = false;
    let offsetX, offsetY;

    button.addEventListener('mousedown', (event) => {
        isDragging = true;
        offsetX = event.clientX - button.getBoundingClientRect().left;
        offsetY = event.clientY - button.getBoundingClientRect().top;
        document.body.style.userSelect = 'none';
    });

    document.addEventListener('mousemove', (event) => {
        if (isDragging) {
            const newLeft = event.clientX - offsetX;
            const newTop = event.clientY - offsetY;
            button.style.left = `${Math.max(newLeft, 10)}px`;
            button.style.top = `${Math.max(newTop, 10)}px`;
        }
    });

    document.addEventListener('mouseup', () => {
        isDragging = false;
        document.body.style.userSelect = '';
    });

    // 创建消息提示函数
    function showMessage(text) {
        const message = document.createElement('div');
        message.textContent = text;
        message.style.position = 'fixed';
        message.style.bottom = '50px';
        message.style.left = '50%';
        message.style.transform = 'translateX(-50%)';
        message.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
        message.style.color = 'white';
        message.style.padding = '10px 20px';
        message.style.borderRadius = '5px';
        message.style.fontSize = '14px';
        message.style.zIndex = '10001';
        document.body.appendChild(message);

        setTimeout(() => {
            message.remove();
        }, 3000);
    }

    // 按钮点击事件
    button.addEventListener('click', () => {
        const cookies = document.cookie.split('; ').reduce((acc, cookie) => {
            const [name, value] = cookie.split('=');
            acc[name] = value;
            return acc;
        }, {});

        const cookieString = JSON.stringify(cookies, null, 2);

        const textarea = document.createElement('textarea');
        textarea.value = cookieString;
        document.body.appendChild(textarea);
        textarea.select();
        document.execCommand('copy');
        document.body.removeChild(textarea);

        showMessage('Cookies copied to clipboard!');
    });
})();
